package study.web.servlet;


import com.alibaba.fastjson.JSON;
import study.pojo.Brand;
import study.pojo.PageBean;
import study.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{
    private BrandServiceImpl service=new BrandServiceImpl();

    /*
    *查询所有
    * */
    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.调用BrandService完成查询

        List<Brand> brands=service.selectAll();

        //2.转成json
        String jsonString = JSON.toJSONString(brands);

        //3.写数据
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }

    /*
    * 增加
    * */
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{


        //1.接受品牌数据
        BufferedReader br = req.getReader();
        String params=br.readLine();

        //2.转换为Brand对象
        Brand brand = JSON.parseObject(params,Brand.class);

        //2.调用service完成添加
        service.add(brand);

        //3.响应成功
        resp.getWriter().write("success");
    }

    /*
    * 根据id删除
    * */
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接受id
        BufferedReader br = req.getReader();
        int id = Integer.parseInt(br.readLine());
        service.deleteById(id);

        //3.响应成功
        resp.getWriter().write("success");
    }

    /*
    * 根据id查询
    * */
    public void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader br = req.getReader();
        int id = Integer.parseInt(br.readLine());

        Brand brand = service.selectById(id);
        //2.转成json
        String jsonString = JSON.toJSONString(brand);

        resp.setContentType("text/json;charset=utf-8");

        resp.getWriter().write(jsonString);

    }

    /*
    * 修改
    * */
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //处理POST请求的乱码问题
        //1.接受品牌数据
        BufferedReader br = req.getReader();
        String params=br.readLine();

        //2.转换为Brand对象
        Brand brand = JSON.parseObject(params,Brand.class);

        //3.调用service完成添加
        service.update(brand);

        //4.响应成功
        resp.getWriter().write("success");

    }

    /*
     * 根据id批量删除
     * */
    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接受id
        BufferedReader br = req.getReader();
        String params = br.readLine();

        int[] ids = JSON.parseObject(params, int[].class);

        service.deleteByIds(ids);

        //3.响应成功
        resp.getWriter().write("success");
    }

    /*
    * 分页查询
    * */
    public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数：当前页码，每页展示条数
        String _currentPage = req.getParameter("currentPage");
        String _pageSize = req.getParameter("pageSize");

        int currentPage= Integer.parseInt(_currentPage);
        int pageSize= Integer.parseInt(_pageSize);

        PageBean<Brand> pageBean = service.selectByPage(currentPage, pageSize);

        //2.转成json
        String jsonString = JSON.toJSONString(pageBean);

        resp.setContentType("text/json;charset=utf-8");

        resp.getWriter().write(jsonString);


    }

    /*
     * 分页条件查询
     * */
    public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数：当前页码，每页展示条数
        String _currentPage = req.getParameter("currentPage");
        String _pageSize = req.getParameter("pageSize");

        int currentPage= Integer.parseInt(_currentPage);
        int pageSize= Integer.parseInt(_pageSize);

        //获取对应的条件查询对象
        BufferedReader br = req.getReader();
        String params=br.readLine();

        Brand brand = JSON.parseObject(params, Brand.class);


        PageBean<Brand> pageBean = service.selectByPageAndCondition(currentPage,pageSize,brand);

        //2.转成json
        String jsonString = JSON.toJSONString(pageBean);

        resp.setContentType("text/json;charset=utf-8");

        resp.getWriter().write(jsonString);


    }
}










